package com.x.param;

import com.x.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ClassName: ParamController
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.09.10 22:35
 */

@Controller
@RequestMapping("param")
public class ParamController {

    @RequestMapping("data1")
    @ResponseBody
    public String data1(String name,int age){
        System.out.println("name = " + name + ", age = " + age);
        return "name = " + name + ", age = " + age;
    }


    /**
     * arg1--->username: @RequestParam(value = "name")此值必须传递，别名为name
     * arg2--->age:      @RequestParam(required = false, defaultValue = "1") 指定此值不必须传递required=false，默认为1
     *
     * value="name" 指定请求参数名,如果形参名和请求参数名一致，可以省略
     * required = false 前端是否必须传递此参数，默认是必须，不传400异常
     * defaultValue = "1" 当非必须传递false，可以设置默认值
     */
    @RequestMapping("data2")
    @ResponseBody
    public String data2(@RequestParam(value = "name") String username,
                        @RequestParam(required = false, defaultValue = "1") int age){
        System.out.println("name = " + username + ", age = " + age);
        return "name = " + username + ", age = " + age;
    }


    //param/data3?info=eat&info=play&info=study

    /**
     * arg--->info List集合类型必须加@RequestParam才能解析, 将对应的字符串正确传入
     */
    @RequestMapping("data3")
    @ResponseBody
    public String data3(@RequestParam List<String> info){
        System.out.println("info = "+info);
        return info.toString();
    }


    //使用实体对象接值     用户的信息 ---> 对应的实体类 ---> 插入到数据库的表
    /**
     * @param user 将实体类放入形参中，传入的实体的属性会自动赋值
     */
    @RequestMapping("data4")
    @ResponseBody
    public String data4(User user){
        System.out.println("User Information: "+user);
        return user.toString();
    }
}
